#!/usr/bin/env python

import time
from httputil import HttpUtil
from config import logger

class NetWork(HttpUtil):
  def __init__(self, prefixURL=None):
    HttpUtil.__init__(self, prefixURL)

  def listZones(self, id):
    """ (count,) """
    command = 'listZones'
    args = {
            "id": id
           }
    response = self.http_get(command, args)
    logger.debug(response)
    return (response["count"],)  

  def createZone(self, networktype, name, dns1, internaldns1):
    """ (id, ) """
    command = 'createZone'
    args = {
             "networktype" : networktype,
             "name"        : name,
             "dns1"        : dns1,
             "internaldns1": internaldns1
           }
    response = self.http_get(command, args)
    logger.debug(response)
    return (response["zone"]["id"],)

  def deleteZone(self, id):
    """ (success, ) """
    command = 'deleteZone'
    args = {
             "id": id
           }
    response = self.http_get(command, args)
    logger.debug(response)
    return (response["success"],)

  def listPods(self, id):
    """ (count,) """
    command = 'listPods'
    args = {
            "id": id
           }
    response = self.http_get(command, args)
    logger.debug(response)
    return (response["count"],)

  def createPod(self, zoneid, name, netmask, startip, endip, gateway):
    """ (id, ) """
    command = 'createPod'
    args = {
             "zoneid" : zoneid,
             "name"   : name,
             "netmask": netmask,
             "startip": startip,
             "endip"  : endip,
             "gateway": gateway
           }
    response = self.http_get(command, args)
    logger.debug(response)
    return (response["pod"]["id"],)

  def deletePod(self, id):
    """ (success, ) """
    command = 'deletePod'
    args = {
             "id": id
           }
    response = self.http_get(command, args)
    logger.debug(response)
    return (response["success"],)

  def listClusters(self, id):
    """ (count,) """
    command = 'listClusters'
    args = {
            "id": id
           }
    response = self.http_get(command, args)
    logger.debug(response)
    return (response["count"],)

  def addCluster(self, zoneid, podid, clustername, clustertype, hypervisor):
    """ (id, ) """
    command = 'addCluster'
    args = {
             "zoneid"     : zoneid,
             "podid"      : podid,
             "clustername": clustername,
             "clustertype": clustertype,
             "hypervisor" : hypervisor
           }
    response = self.http_get(command, args)
    logger.debug(response)
    return (response["cluster"][0]["id"],)

  def deleteCluster(self, id):
    """ (success, ) """
    command = 'deleteCluster'
    args = {
             "id": id
           }
    response = self.http_get(command, args)
    logger.debug(response)
    return (response["success"],)

  def listHosts(self, id):
    """ (count,) """
    command = 'listHosts'
    args = {
            "id": id
           }
    response = self.http_get(command, args)
    logger.debug(response)
    return (response["count"],)

  def addHost(self, zoneid, podid, clusterid, username, password, url, hypervisor):
    """ (id, ) """
    command = 'addHost'
    args = {
             "zoneid"     : zoneid,
             "podid"      : podid,
             "clusterid"  : clusterid,
             "username"   : username,
             "password"   : password,
             "url"        : url,
             "hypervisor" : hypervisor
           }
    response = self.http_get(command, args)
    logger.debug(response)
    return (response["host"][0]["id"],)

  def deleteHost(self, id):
    """ (success, ) """
    command = 'deleteHost'
    args = {
             "id": id
           }
    response = self.http_get(command, args)
    logger.debug(response)
    return (response["success"],)

  def prepareHostForMaintenance(self, id):
    """ (success, ) """
    command = 'prepareHostForMaintenance'
    args = {
             "id": id
           }
    response = self.http_get(command, args, async=True)
    logger.debug(response)
    state = response["host"]["state"]
    running = True
    refresh = False
    while running:
      if state == 'PrepareForMaintenance':
        time.sleep(3)
        response = self.http_get("listHosts", args)
        state = response["host"][0]["state"]
        refresh = True
      elif state == 'Maintenance':
        running = False
    else:
      if refresh == True:
        return (response["host"][0]["state"],)
      else:
        return (response["host"]["state"],)

  def listOsTypes(self, id):
    """ (count,) """
    command = 'listOsTypes'
    args = {
            "id": id
           }
    response = self.http_get(command, args)
    logger.debug(response)
    return (response["count"],)

  def listAccounts(self, id):
    """ (count,) """
    command = 'listAccounts'
    args = {
            "id": id
           }
    response = self.http_get(command, args)
    logger.debug(response)
    return (response["count"],)

  def createAccount(self, username, password, email, firstname, lastname, accounttype, domainid):
    """ (id, ) """
    command = 'createAccount'
    data = {
             "username"    : username,
             "password"   : password,
             "email"      : email,
             "firstname"  : firstname,
             "lastname"   : lastname,
             "accounttype": accounttype,
             "domainid"   : domainid
           }
    response = self.http_get(command, data)
    logger.debug(response)
    return (response["account"]["id"],)

  def deleteAccount(self, id):
    """ (success, ) """
    command = 'deleteAccount'
    args = {
             "id": id
           }
    response = self.http_get(command, args, async=True)
    logger.debug(response)
    return (response["success"],)

  def listVlanIpRanges(self, zoneid, podid, vlan):
    """ (count,) """
    command = 'listVlanIpRanges'
    args = {
             "zoneid": zoneid,
             "podid" : podid,
             "vlan"  : vlan
           }
    response = self.http_get(command, args)
    logger.debug(response)
    return (response["count"],)

  def createVlanIpRange(self, zoneid, podid, vlan, forvirtualnetwork, startip, endip, netmask, gateway):
    """ (id, ) """
    command = 'createVlanIpRange'
    args = {
             "zoneid"            : zoneid,
             "podid"             : podid,
             "vlan"              : vlan,
             "forvirtualnetwork" : forvirtualnetwork,
             "startip"           : startip,
             "endip"             : endip,
             "netmask"           : netmask,
             "gateway"           : gateway
           }
    response = self.http_get(command, args)
    logger.debug(response)
    return (response["vlan"]["id"],)

  def deleteVlanIpRange(self, id):
    """ (success, ) """
    command = 'deleteVlanIpRange'
    args = {
             "id": id
           }
    response = self.http_get(command, args)
    logger.debug(response)
    return (response["success"],)

  def listSecurityGroups(self, id):
    """ (count,) """
    command = 'listSecurityGroups'
    args = {
            "id": id
           }
    response = self.http_get(command, args)
    logger.debug(response)
    return (response["count"],)

  def createSecurityGroup(self, name, account, domainid):
    """ (id, ) """
    command = 'createSecurityGroup'
    data = {
             "name"    : name,
             "account" : account,
             "domainid": domainid
           }
    response = self.http_get(command, data)
    logger.debug(response)
    return (response["securitygroup"]["id"],)

  def deleteSecurityGroup(self, id):
    """ (success, ) """
    command = 'deleteSecurityGroup'
    args = {
             "id": id
           }
    response = self.http_get(command, args)
    logger.debug(response)
    return (response["success"],)

  def authorizeSecurityGroupIngress(self, securitygroupid, protocol, startport, endport, cidrlist):
    """ (id, ) """
    command = 'authorizeSecurityGroupIngress'
    args = {
             "securitygroupid" : securitygroupid,
             "protocol"        : protocol,
             "startport"       : startport,
             "endport"         : endport,
             "cidrlist"        : cidrlist
           }
    response = self.http_get(command, args, async=True)
    logger.debug(response)
    return (response["securitygroup"]["ingressrule"][0]["ruleid"],)

  def revokeSecurityGroupIngress(self, id):
    """ (success, ) """
    command = 'revokeSecurityGroupIngress'
    args = {
             "id": id
           }
    response = self.http_get(command, args, async=True)
    logger.debug(response)
    return (response["success"],)
